﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Model
{
    /// <summary>
    /// 入口
    /// </summary>
    public abstract class Entity : Disposer
    {
        // 父实体
        private Entity Parent { get; set; }

        // 类型和组件对应
        private Dictionary<string, Component> componentDict = new Dictionary<string, Component>();


        protected Entity()
        {

        }

        protected Entity(long id)
        {
            this.Id = id;
        }

        /// <summary>
        /// 添加组件
        /// </summary>
        /// <typeparam name="T">组件类型</typeparam>
        /// <returns>组件实例</returns>
        public T AddComponent<T>(string name = null) where T : Component, new()
        {
            T component = ComponentFactory.Create<T>(this);
            try
            {
                name = name ?? typeof(T).Name;
                componentDict.Add(name, component);
                return component;
            }
            catch (Exception)
            {
                Log.Error($"key 已经存在 Type:{typeof(T)}");
            }
            return null;
        }

        /// <summary>
        /// 添加组件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="A"></typeparam>
        /// <param name="a"></param>
        /// <returns></returns>
        public T AddComponent<T, A>(A a,string name = null) where T : Component, new()
        {
            T component = ComponentFactory.Create<T, A>(this, a);
            try
            {
                name = name ?? typeof(T).Name;
                componentDict.Add(name, component);
                return component;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 添加组件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="A"></typeparam>
        /// <typeparam name="B"></typeparam>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public T AddComponent<T, A,B>(A a,B b,string name = null) where T : Component, new()
        {
            T component = ComponentFactory.Create<T, A, B>(this, a, b);
            try
            {
                name = name ?? typeof(T).Name;
                componentDict.Add(name, component);
                return component;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 获取组件
        /// </summary>
        /// <typeparam name="T">组件类型</typeparam>
        /// <returns>组件实例</returns>
        public T GetComponent<T>(string name = null) where T : Component,new()
        {
            name = name ?? typeof(T).Name;

            if (componentDict.ContainsKey(name))
            {
                return componentDict[name] as T;
            }
            return null;
        }

        /// <summary>
        /// 获取所有组件
        /// </summary>
        /// <returns></returns>
        public List<Component> GetComponentAll()
        {
            return this.componentDict.Values.ToList();
        }

        /// <summary>
        /// 移除组件
        /// </summary>
        /// <typeparam name="T">组件类型</typeparam>
        public void RemoveComponent<T>(string name = null) where T : Component,new()
        {
            var t = typeof(T);
            name = name ?? t.Name;
            if (componentDict.ContainsKey(name))
            {
                var comp = componentDict[name];
                componentDict.Remove(name);
            }
        }

        /// <summary>
        /// 组件是否存在
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        public bool HasComponent<T>(string name=null) where T :Component,new()
        {
            name = typeof(T).Name;
            return componentDict.ContainsKey(name);
        }
    }
}